import 'package:cat_eye/base/base_chang_notifier.dart';
import 'package:cat_eye/base/base_refresh_view_model.dart';
import 'package:cat_eye/provider/provider_widget.dart';
import 'package:cat_eye/util/loading_state_widget_util.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

/*
  * @Package: base
  * @ClassName: base_state
  * @Author: szj
  * @CreateDate: 8/2/21 10:09 AM
  *
 * AutomaticKeepAliveClientMixin 作用:
 *  切换tab保留tab的状态，避免initState()重复调用
 *  1. 比如重写wantKeepAlive 并且返回true
 *  2. build 必须返回super
 *
 * W  StatefulWidget 状态
 * VM ViewModel
 *
 * TODO 上拉加载,下拉刷新封装的BaseState
 */
abstract class BaseRefreshState<W extends StatefulWidget,
        VM extends BaseRefreshViewModel> extends State<W>
    with AutomaticKeepAliveClientMixin {
  //继承类传过来
  VM get buildViewModel;

  Widget buildWidget(BuildContext context, VM model);

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return ProviderWidget<VM>(
        //第一次加载数据
        onModelInit: (m) => m.refresh(),
        model: buildViewModel,
        builder: (context, model, child) {
          return LoadingWidget(
              reload: model.retry,
              type: model.type,
              child: SmartRefresher(
                controller: model.refreshController,
                onRefresh: model.refresh,
                onLoading: model.loadMore,
                //允许上拉加载
                enablePullUp: model.isLoadMore,
                //允许下拉刷新
                enablePullDown: model.isRefresh,
                //子State具体实现
                child: buildWidget(context, model),
              ));
        });
  }

  @override
  bool get wantKeepAlive => true;
}
